<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 功能1：获取root_list的uid并将详细信息加载到html页面，并可修改(完成) -->
    <title>管理员权限-用户详细信息</title>
</head>
<body onload="getUser()">
        <h1>用户详细信息</h1>
        用户名<input type="text" name="uname" id="uname" value="" disabled><br>
        密码<input type="text" name="upwd" id="upwd" value=""><br>
        邮箱<input type="text" name="email" id="email" value=""><br>
        电话<input type="phone" name="phone" id="phone" value=""><br>
        真实姓名<input type="text" name="user_name" id="user_name" value=""><br>
        宠物姓名<input type="text" name="Pets_name" id="Pets_name" value=""><br>
        <input type="button" name="btn" id="btn" value="修改" onclick="update()">
    <script>
            //按uid查询
            function getUser(){
            //把查询字符串封装成对象
            var obj=new URLSearchParams(location.search);
            //通过对象提取查询字符串中的参数
           console.log(obj);
            var _uid=obj.get('uid');
           console.log(_uid);
            var xhr =new XMLHttpRequest();
            xhr.onreadystatechange=function(){
                if(xhr.readyState===4&&xhr.status===200){
                var r=xhr.responseText;
                var obj=JSON.parse(r);
                uname.value=obj.data[0].uname;
                upwd.value=obj.data[0].upwd;
                email.value=obj.data[0].email;
                phone.value=obj.data[0].phone;
                user_name.value=obj.data[0].user_name;
                Pets_name.value=obj.data[0].Pets_name;
                //console.log(r);
                //console.log(obj.data[0].uname);
            }}
            var url=`http://127.0.0.1:8080/a1/users/get/?uid=${_uid}`
            xhr.open('GET',url,true);
            xhr.send();
            }
            //修改
            function update(){
                //将查询字符串封装成对象
                var obj=new URLSearchParams(location.search);
                //获取数据
                console.log(obj);
                var _uid=obj.get('uid');
                console.log(_uid);
                var _uname=uname.value;
                var _upwd=upwd.value;
                var _email=email.value;
                var _phone=phone.value;
                var _user_name=user_name.value;
                var _Pets_name=Pets_name.value;
                //创建xhr对象
                var xhr=new XMLHttpRequest();
                //设置状态码相应
                xhr.onreadystatechange=function(){
                    //判断响应码
                    if(xhr.readyState===4&&xhr.status===200){
                    //将返回值转换为JS对象
                    var res=JSON.parse(xhr.responseText);
                    //判断返回值的code属性值是否为200，为200的话跳出弹框显示修改成功并跳转root_list页面
                    if(res.code==200){
                        alert('修改成功');
                        location.href="root_list.html"
                    }else{
                        alert('修改失败，请重试');
                    }
                }}
                //打开链接
                var url="/a1/users/"
                xhr.open('PUT',url,true);
                //修改请求头信息
                xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                //发送数据
                var formdata=`uid=${_uid}&uname=${_uname}&upwd=${_upwd}&email=${_email}&phone=${_phone}&user_name=${_user_name}&Pets_name=${_Pets_name}`
                xhr.send(formdata);
            }
    </script>
</body>
</html>